package com.jwiki.app.jkeeper.config;

import java.io.File;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("jetty")
public class JettyServerConfigurer {

    private int port;

    private String resourceBase;

    private boolean directoriesListed;

    @Bean(initMethod = "start", destroyMethod = "stop")
    public Server server() {
        File root = new File(resourceBase);
        if (!root.exists()) {
            root.mkdirs();
        }
        Server server = new Server(port);
        ResourceHandler handler = new ResourceHandler();
        handler.setResourceBase(resourceBase);
        handler.setDirectoriesListed(directoriesListed);
        server.setHandler(handler);
        return server;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getResourceBase() {
        return resourceBase;
    }

    public void setResourceBase(String resourceBase) {
        this.resourceBase = resourceBase;
    }

    public boolean isDirectoriesListed() {
        return directoriesListed;
    }

    public void setDirectoriesListed(boolean directoriesListed) {
        this.directoriesListed = directoriesListed;
    }

}
